import java.io.*;
import java.util.*;
import org.wikipedia.*;
import javax.security.auth.login.*;
import java.net.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.imageio.*;
import java.awt.image.*;

public class VFD 
{
  //fields.  Current methods are rather sloppy.  This should be phased out. 
  public static Wiki wiki = new Wiki();
  public static String currentfile;
  public static JTextArea a = new JTextArea(20, 40);
  public static JComboBox selector;
  public static String date;
  public static ArrayList<String> files;
  public static JPanel center;

  public static void main (String[] args) throws FailedLoginException, IOException, LoginException, CredentialExpiredException, AccountLockedException, CredentialNotFoundException, FileNotFoundException
  {
    guiLogin(wiki, "VFFD - login");
    wiki.setMaxLag(100);
    wiki.setThrottle(1);

    date = "Wikipedia:Files for deletion/" + getDate();
    Scanner m = new Scanner(wiki.getPageText(date));
    String l;
    files = new ArrayList<String>();
    while (m.hasNextLine())
     if ((l = m.nextLine()).startsWith("====[[:"))
	files.add(l.substring(7, l.indexOf("]]====")));
     
    if (files.size() == 0)
    {
	JOptionPane.showConfirmDialog(null, "No items exist for this day.  Program will now exit");
       System.exit(1);
    }


    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setTitle("VFFD");
    frame.setLayout(new BorderLayout());
    
    JPanel north = new JPanel(new BorderLayout());
    center = new JPanel(new BorderLayout());

    north.add(new JLabel("Jump to:"), BorderLayout.WEST);
    selector = new JComboBox(files.toArray(new String[0]));
    selector.setSelectedIndex(0);

    selector.addActionListener(new AbstractAction() {
       @Override
        public void actionPerformed(ActionEvent e ) {
           currentfile = (String) selector.getSelectedItem();
	    try
	    {
	      a.setText(wiki.getSectionText(date, files.indexOf(currentfile) + 2));
	      setImageField(center, currentfile);
	    }
	    catch (IOException ex)
	    {
	      System.out.println("Experienced network issues, program may not have updated box properly");
	    }
        }});
    
    north.add(selector, BorderLayout.EAST);
    
    setImageField(center, files.get(0));
    
    frame.add(north, BorderLayout.NORTH);
    frame.add(center, BorderLayout.CENTER);
    frame.setVisible(true);
   
	
    
  }

  public static void setImageField(JPanel p, String file) throws IOException
  {
    if (file.endsWith("jpg") || file.endsWith("JPG") || file.endsWith("JPEG") || file.endsWith("jpeg") || file.endsWith("png") || file.endsWith("PNG") || file.endsWith("GIF") || file.endsWith("gif"))
    {
      BufferedImage img = ImageIO.read(new ByteArrayInputStream(wiki.getImage(namespaceStrip(file).replace(" ", "_"), 200, 200)));
      JLabel picLabel = new JLabel(new ImageIcon(img));
      p.add(picLabel, BorderLayout.NORTH);
      p.add(new JLabel("Dimensions: " + img.getHeight() + " x " + img.getWidth()), BorderLayout.SOUTH);

    }
    else
    {
     p.add(new JLabel("No preview available.  Unsupported filetype"), BorderLayout.NORTH);
     p.add(new JLabel(""), BorderLayout.SOUTH);
    }

  }

  public static String getDate()
  {
   GregorianCalendar utc = new GregorianCalendar();
   utc.setTimeZone(TimeZone.getTimeZone("UTC"));

   long[] rd = new long[9];
   for (int i = 0; i < 9; i++)
     rd[i] = utc.getTime().getTime() - 86400000L*i;

   String[] dates = new String[9];
   for(int i = 0; i < 9; i ++)
   {
     utc.setTimeInMillis(rd[i]);
     dates[i] = "" + utc.get(Calendar.YEAR) + " " + getMonthText(utc.get(Calendar.MONTH)) + " " + utc.get(Calendar.DAY_OF_MONTH);
   }

    JPanel p = new JPanel(new GridLayout(3,1));
    JComboBox b = new JComboBox(dates);
    b.setEditable(true);
    p.add(new JLabel("Select a day:"));
    p.add(b);
    p.add(new JLabel("New Nominations at " + dates[0]));
    int ok = JOptionPane.showConfirmDialog(null, p, "VFFDN - Choose a day", JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
    if (ok != JOptionPane.OK_OPTION)
       System.exit(1);

    return (String) b.getSelectedItem();
}

public static String getMonthText(int d)
{
  switch(d)
  {
    case 0 : return "January";
    case 1: return "February";
    case 2: return "March";
    case 3: return "April";
    case 4: return "May";
    case 5: return "June";
    case 6: return "July";
    case 7: return "August";
    case 8: return "September";
    case 9: return "October";
    case 10: return "November";
    case 11: return "December";
    default : return null;
  }
} 

  public static void guiLogin(Wiki wiki, String title) throws IOException, FailedLoginException
  {

    JPanel pl = new JPanel(new GridLayout(2,2));
    pl.add(new JLabel("Username:"));
    JTextField u = new JTextField(12);
    pl.add(u);
    pl.add(new JLabel("Password:"));
    JPasswordField px = new JPasswordField(12);
    pl.add(px);

    int ok = JOptionPane.showConfirmDialog(null, pl, title, JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
    if (ok != JOptionPane.OK_OPTION)
         System.exit(1);
    try
    {
      wiki.login(u.getText().trim(), px.getPassword());
      wiki.setThrottle(1); //user editing, they can go as fast as they want
      wiki.setMaxLag(200); //no reason for user to be affected by server lag
    }
    catch (FailedLoginException ex)
    {
       JOptionPane.showMessageDialog(null, "Login Failed, program will now exit");
       System.exit(1);
    }
  }

 //strips the namespace from a title
 public static String namespaceStrip(String title)
 {
   try
   {
     return title.substring(title.indexOf(":") + 1);
   }
   catch (StringIndexOutOfBoundsException e)
   {
     //Don't crash program
   }
   return title;
 }

}


   /////Buttons that should be added://////
//Vote Delete
//Vote Keep
//Move to Commons
//Save
//Next File